package xm.dao;

import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

import xm.vo.CommentVO;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class CommentDAO {

private static SqlMapClient sql;
	static{
		try {
			Reader rd = Resources.getResourceAsReader("SqlMapConfig.xml");
			sql = SqlMapClientBuilder.buildSqlMapClient(rd);
		} catch (Exception e) {
			System.out.println("SqlMapClient error : ");
			e.printStackTrace();
		}
	}
	
	//insert
	public static void cmtInsert(CommentVO vo){
		try {
			sql.insert("cmtInsert", vo);
		} catch (Exception e) {System.out.println("cmtInsert error : ");e.printStackTrace();}
	}
	
	//list
	public static ArrayList<CommentVO> getCmtList(Map<String, Object> map){
		ArrayList<CommentVO> list = null;
		try {
			list = (ArrayList<CommentVO>)sql.queryForList("cmtList", map);
		} catch (Exception e) {System.out.println("cmtList error : ");e.printStackTrace();}
		return list;
	}
	
	//total
	public static int getTotalCount(int bnum){
		int value = 0;
		try {
			value = (Integer)sql.queryForObject("cmtTotalCount",bnum);
		} catch (SQLException e) { System.out.println("TotalCount error : "); e.printStackTrace();	}
		
		return value;
	}
	
	//delete & Modify
	public static Boolean getPassCheck(Map<String, Object> map, int no, String cmd){
		Boolean value = false;
		String temp = null;
		
		try {
			temp = (String) sql.queryForObject("cmtPass",map);
			if(temp != null) {
				if(cmd.equals("modify")){
					sql.update("cmtModify",map);
				}else if(cmd.equals("delete")){
					sql.delete("cmtDel",no);
				}
				value = true;
			}
			
		} catch (SQLException e) { System.out.println("delte & modify error : "); e.printStackTrace(); }
	
		return value;
	}
	
	// modfiyForm info
	public static CommentVO getInfo(int no){
		CommentVO vo = null;
		try {
			vo = (CommentVO)sql.queryForObject("cmtInfo",no);
		} catch (SQLException e) { System.out.println("modify form error : "); e.printStackTrace();	}
		return vo;
	}
	
}
